<?php

/**
 * AuthRule(菜单管理)
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class AuthRuleController extends AdminbaseController {

  
    
    function _initialize() {
        parent::_initialize();
        $this->module="Admin";
        $this->db = D("AuthRule");
        
    }

    /**
     *  显示菜单
     */
    public function index() {
    	
    	
    	
   
        $result = $this->db->where(array("module"=> $this->module))->order(array("listorder" => "DESC"))->select();
        import("Tree");
        $tree = new \Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        
        
        $newmenus=array();
        foreach ($result as $m){
        	$newmenus[$m['id']]=$m;
        	 
        }
        foreach ($result as $n=> $r) {
        	
        	$result[$n]['level'] = $this->_get_level($r['id'], $newmenus);
        	$result[$n]['parentid_node'] = ($r['parentid']) ? ' class="child-of-node-' . $r['parentid'] . '"' : '';
        	
            $result[$n]['str_manage'] = '<a  data-toggle="tooltip" title="添加子菜单" class="btn btn-xs btn-success tooltip-success"  href="' . U("AuthRule/add", array("parentid" => $r['id'])) . '"><i  class="icon-plus"></i></a> <a data-toggle="tooltip" title="修改" class="btn btn-xs btn-info tooltip-info" href="' . U("AuthRule/edit", array("id" => $r['id'])) . '"><i class="icon-edit" ></i></a> <a data-toggle="tooltip" title="删除"  class="ajax-del btn btn-xs btn-danger	tooltip-danger"  href="' . U("AuthRule/delete", array("id" => $r['id']) ). '"><i class="icon-trash" ></i></a> ';
                
            $result[$n]['display'] = $r['display'] ?  '<a href="'.U("change",array("field"=>"display","value"=>"0","id"=>$r[id])).'" class="ajax-switch"><i class="icon-check-circle"></i></a>': '<a href="'.U("change",array("field"=>"display","value"=>"1","id"=>$r[id],)).'" class="ajax-switch"><i class="icon-times-circle"></i></a>';
        
            $result[$n]['right'] = $r['right'] ?  '<a href="'.U("change",array("field"=>"right","value"=>"0","id"=>$r[id])).'" class="ajax-switch"><i class="icon-check-circle"></i></a>': '<a href="'.U("change",array("field"=>"right","value"=>"1","id"=>$r[id],)).'" class="ajax-switch"><i class="icon-times-circle"></i></a>';
            
            
        }

        $tree->init($result);
        $str = "<tr id='node-\$id' \$parentid_node>
					<td ><input  name='listorders[\$id]' type='text' size='4' value='\$listorder' class='input input-order'></td>
					<td class='hidden-480'>\$id</td>
        			<td>\$spacer\$title</td>
					<td class='hidden-780'>\$name</td>
				    <td class='hidden-480'>\$display</td>
        		<td class='hidden-480'>\$right</td>
					<td>\$str_manage</td>
				</tr>";
        $categorys = $tree->get_tree(0, $str);
        $this->assign("categorys", $categorys);
        $this->display();
    }
    
    /**
     * 获取菜单深度
     * @param $id
     * @param $array
     * @param $i
     */
    protected function _get_level($id, $array = array(), $i = 0) {
    
    	if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){
    		return  $i;
    	}else{
    		$i++;
    		return $this->_get_level($array[$id]['parentid'],$array,$i);
    	}
    
    }
    
    public function lists(){
    
    	$result = $this->db->where(array("module"=>$this->module))->order(array("name" => "ASC"))->select();
    	$this->assign("menus",$result);
    	$this->display();
    }

    /**
     *  添加
     */
    public function add() {
    	
    	if (IS_POST) {
    		$_POST[module]=$this->module;
    		
    		if ($this->db->create()) {
    			if ($this->db->add()!==false) {
    				$this->db->data_cache();
    				$this->success("添加成功！");
    			} else {
    				$this->error("添加失败！");
    			}
    		} else {
    			$this->error($this->db->getError());
    		}
    	}
    	else
    	{
    	import("Tree");
    	$tree = new \Tree();
    	$parentid = intval(I("get.parentid"));
    	$result = $this->db->order(array("listorder" => "DESC"))->select();
    	foreach ($result as $r) {
    		$r['selected'] = $r['id'] == $parentid ? 'selected' : '';
    		$array[] = $r;
    	}
    	$str = "<option value='\$id' \$selected>\$spacer \$title</option>";
    	$tree->init($array);
    	$select_categorys = $tree->get_tree(0, $str);
    	$this->assign("select_categorys", $select_categorys);
    	$this->display('');
    	
    	}
    }
    

    /**
     *  删除
     */
    public function delete() {
        $id = intval(I("get.id"));
        $count = $this->db->where(array("parentid" => $id))->count();
        if ($count > 0) {
            $this->error("该菜单下还有子菜单，无法删除！");
        }
        if ($this->db->delete($id)!==false) {
        	$this->db->data_cache();
            $this->success("删除菜单成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     *  编辑
     */
    public function edit() {
    	
    	if (IS_POST) {
    		if ($this->db->create()) {
    			if ($this->db->save() !== false) {
    				$this->db->data_cache();
    				$this->success("更新成功！",U("index"));
    			} else {
    				$this->error("更新失败！");
    			}
    		} else {
    			$this->error($this->db->getError());
    		}
    	}
    	
    	{
        import("Tree");
        $tree = new \Tree();
        $id = intval(I("get.id"));
        $rs = $this->db->where(array("id" => $id))->find();
        $result = $this->db->order(array("listorder" => "DESC"))->select();
        foreach ($result as $r) {
        	$r['selected'] = $r['id'] == $rs['parentid'] ? 'selected' : '';
        	$array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$title</option>";
        $tree->init($array);
        $select_categorys = $tree->get_tree(0, $str);
        $this->assign("data", $rs);
        $this->assign("select_categorys", $select_categorys);
        $this->display();
        
    	}
    }
    
   

	// 排序
	public function listorders() {
		$status = parent::_listorders ( $this->db );
		if ($status) {
			
			$this->db->data_cache();
			$this->success ( "排序更新成功！" );
		} else {
			$this->error ( "排序更新失败！" );
		}
	}
    

    
    

}

?>
